(function ($, window, document) {
    // 定义构造函数
    function Paging(el, options) {
        this.el = el;
        this.options = {
            pageNo: options.initPageNo || 1, // 初始页码
            totalPages: options.totalPages || 1, //总页数
            totalCount: options.totalCount || '', // 条目总数
            firstname: options.firstname || '首页', //首页名称
            lastname: options.lastname || '尾页', //尾页名称
            prename: options.prename || '上一页', //上一页名称
            nextname: options.nextname || '下一页', //下一页名称
            jumpname: options.jumpname || '跳转', //跳转名称
            totalname: options.totalname || '共', //共 名称
            pagename: options.pagename || '页', //页 名称
            slideSpeed: options.slideSpeed || 0, // 缓动速度
            jump: options.jump || false, // 支持跳转
            callback: options.callback || function () {} // 回调函数
        };
        this.init();
    }
    // 给实例对象添加公共属性和方法
    Paging.prototype = {
        constructor: Paging,
        init: function () {
            this.createDom();
            this.bindEvents();
        },
        createDom: function () {
            var that = this,
                    ulDom = '',
                    jumpDom = '',
                    content = '',
                    liWidth = 30, // li的宽度
                    totalPages = that.options.totalPages, // 总页数
                    wrapLength = 0;
            totalPages > 5 ? wrapLength = 5 * liWidth : wrapLength = totalPages * liWidth;
            for (var i = 1; i <= that.options.totalPages; i++) {
                i != 1 ? ulDom += '<li>' + i + '</li>' : ulDom += '<li class="sel-page">' + i + '</li>';
            }
            that.options.jump ? jumpDom = '<input type="text" placeholder="1" class="jump-text" id="jumpText"><button type="button" class="jump-button" id="jumpBtn">' + that.options.jumpname + '</button>' : jumpDom = '';
            content = '<button type="button" id="firstPage" class="turnPage first-page">' + that.options.firstname + '</button>' +
                    '<button class="turnPage" id="prePage">' + that.options.prename + '</button>' +
                    '<div class="pageWrap" style="width:' + wrapLength + 'px">' +
                    '<ul id="pageSelect" style="transition:all ' + that.options.slideSpeed + 'ms">' +
                    ulDom +
                    '</ul></div>' +
                    '<button class="turnPage" id="nextPage">' + that.options.nextname + '</button>' +
                    '<button type="button" id="lastPage" class="last-page">' + that.options
                    .lastname + '</button>' +
                    jumpDom +
                    '<p class="total-pages">' + that.options.totalname + '&nbsp;' +
                    that.options.totalPages +
                    '&nbsp;' + that.options.pagename + '</p>' +
                    '<p class="total-count">' +
                    that.options.totalCount +
                    '</p>';
            that.el.html(content);
        },
        bindEvents: function () {
            var that = this,
                    pageSelect = $('#pageSelect'), // ul
                    lis = pageSelect.children(), // li的集合
                    liWidth = lis[0].offsetWidth, // li的宽度
                    totalPages = that.options.totalPages, // 总页数
                    pageIndex = that.options.pageNo, // 当前选择的页码
                    distance = 0, // ul移动距离
                    prePage = $('#prePage'),
                    nextPage = $('#nextPage'),
                    firstPage = $('#firstPage'),
                    lastPage = $('#lastPage'),
                    jumpBtn = $('#jumpBtn'),
                    jumpText = $('#jumpText');

            prePage.on('click', function () {
                pageIndex--;
                if (pageIndex < 1)
                    pageIndex = 1;
                handles(pageIndex);
                if (window.location.href.indexOf("?") >= 0) {
                    var oldp = GetQueryString("p");
                    if (oldp > 0) {
                        var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                        console.log(url);
                        if (url == window.location.href) {
                            url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                        }
                        window.location.href = url;
                    } else {
                        var url = window.location.href + '&p=' + pageIndex;
                        window.location.href = url;
                    }
                } else {
                    console.log(window.location.href + '?p=' + pageIndex);
                    window.location = window.location.href + '?p=' + pageIndex;
                }
            })

            nextPage.on('click', function () {
                pageIndex++;
                if (pageIndex > lis.length)
                    pageIndex = lis.length;
                handles(pageIndex);
                if (window.location.href.indexOf("?") >= 0) {
                    var oldp = GetQueryString("p");
                    if (oldp > 0) {
                        var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                        console.log(url);
                        if (url == window.location.href) {
                            url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                        }
                        window.location.href = url;
                    } else {
                        var url = window.location.href + '&p=' + pageIndex;
                        window.location.href = url;
                    }
                } else {
                    console.log(window.location.href + '?p=' + pageIndex);
                    window.location = window.location.href + '?p=' + pageIndex;
                }
            })

            firstPage.on('click', function () {
                pageIndex = 1;
                handles(pageIndex);
                if (window.location.href.indexOf("?") >= 0) {
                    var oldp = GetQueryString("p");
                    if (oldp > 0) {
                        var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                        console.log(url);
                        if (url == window.location.href) {
                            url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                        }
                        window.location.href = url;
                    } else {
                        var url = window.location.href + '&p=' + pageIndex;
                        window.location.href = url;
                    }
                } else {
                    console.log(window.location.href + '?p=' + pageIndex);
                    window.location = window.location.href + '?p=' + pageIndex;
                }
            })

            lastPage.on('click', function () {
                pageIndex = totalPages;
                handles(pageIndex);
                if (window.location.href.indexOf("?") >= 0) {
                    var oldp = GetQueryString("p");
                    if (oldp > 0) {
                        var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                        console.log(url);
                        if (url == window.location.href) {
                            url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                        }
                        window.location.href = url;
                    } else {
                        var url = window.location.href + '&p=' + pageIndex;
                        window.location.href = url;
                    }
                } else {
                    console.log(window.location.href + '?p=' + pageIndex);
                    window.location = window.location.href + '?p=' + pageIndex;
                }
            })

            jumpBtn.on('click', function () {
                var jumpNum = parseInt(jumpText.val().replace(/\D/g, ''));
                if (jumpNum && jumpNum >= 1 && jumpNum <= totalPages) {
                    pageIndex = jumpNum;
                    handles(pageIndex);
                    jumpText.val(jumpNum);
                    if (window.location.href.indexOf("?") >= 0) {
                        var oldp = GetQueryString("p");
                        if (oldp > 0) {
                            var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                            console.log(url);
                            if (url == window.location.href) {
                                url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                            }
                            window.location.href = url;
                        } else {
                            var url = window.location.href + '&p=' + pageIndex;
                            window.location.href = url;
                        }
                    } else {
                        console.log(window.location.href + '?p=' + pageIndex);
                        window.location = window.location.href + '?p=' + pageIndex;
                    }
                }
            })

            lis.on('click', function () {
                pageIndex = $(this).index() + 1;
                handles(pageIndex);

                if (window.location.href.indexOf("?") >= 0) {
                    var oldp = GetQueryString("p");
                    console.log(oldp);
                    if (oldp > 0) {
                        var url = window.location.href.replace("?p=" + oldp, '?p=' + pageIndex);
                        console.log(url);
                        if (url == window.location.href) {
                            url = window.location.href.replace("&p=" + oldp, '&p=' + pageIndex);
                        }
                        window.location.href = url;
                    } else {
                        var url = window.location.href + '&p=' + pageIndex;
                        window.location.href = url;
                    }
                } else {
                    console.log(window.location.href + '?p=' + pageIndex);
                    window.location = window.location.href + '?p=' + pageIndex;
                }

            })


            //获取url某个参数的值
            function GetQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null)
                    return unescape(r[2]);
                return null;
            }

            function handles(pageIndex) {
                lis.removeClass('sel-page').eq(pageIndex - 1).addClass('sel-page');
                if (totalPages <= 5) {
                    that.options.callback(pageIndex);
                    return false;
                }
                if (pageIndex >= 3 && pageIndex <= totalPages - 2)
                    distance = (pageIndex - 3) * liWidth;
                if (pageIndex == 2 || pageIndex == 1)
                    distance = 0;
                if (pageIndex > totalPages - 2)
                    distance = (totalPages - 5) * liWidth;
                pageSelect.css('transform', 'translateX(' + (-distance) + 'px)');
                pageIndex == 1 ? firstPage.attr('disabled', true) : firstPage.attr('disabled', false);
                pageIndex == 1 ? prePage.attr('disabled', true) : prePage.attr('disabled', false);
                pageIndex == totalPages ? lastPage.attr('disabled', true) : lastPage.attr('disabled', false);
                pageIndex == totalPages ? nextPage.attr('disabled', true) : nextPage.attr('disabled', false);
                that.options.callback(pageIndex);
            }

            handles(that.options.pageNo); // 初始化页码位置
        }
    }
    $.fn.paging = function (options) {
        return new Paging($(this), options);
    }
})(jQuery, window, document);